/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mvp.levelup.jdbc.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;
import mvp.levelup.jdbc.model.TestUser;

/**
 *
 * @author WORKSPOT2
 */
public class TestUserDao
{

    public List<TestUser> getAllUsersFromDB()
    {
        List<TestUser> toReturn = new LinkedList<TestUser>();
        try
        {
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try
            {
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/levelup_test", "root", "root");
                ps = conn.prepareStatement("Select * from levelup_test.test_table1");
                rs = ps.executeQuery();
                TestUser tu = null;
                while (rs.next())
                {
                    tu = new TestUser(rs.getInt("id"), rs.getString("name"), rs.getInt("age"), rs.getInt("account_id"));
                    toReturn.add(tu);
                }
            }
            finally
            {
                if (conn != null)
                {
                    conn.close();
                    conn = null;
                }
                if (ps != null)
                {
                    ps.close();
                    ps = null;
                }
                if (rs != null)
                {
                    rs.close();
                    rs = null;
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return toReturn;
    }

    public boolean addUserToDB(TestUser user)
    {
        boolean f = false;
        try
        {
            Connection conn = null;
            PreparedStatement ps = null;
            try
            {
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/levelup_test", "root", "root");
                ps = conn.prepareStatement("Insert into levelup_test.test_table1 VALUES(?,?,?,?)");
                ps.setInt(1, user.getId());
                ps.setString(2, user.getName());
                ps.setInt(3, user.getAge());
                ps.setInt(4, user.getAccount_id());
                int res = ps.executeUpdate();
                f = res > 0;
            }
            finally
            {
                if (conn != null)
                {
                    conn.close();
                    conn = null;
                }
                if (ps != null)
                {
                    ps.close();
                    ps = null;
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return f;
    }
}
